package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.CourseMapper;
import com.think.teaching.model.Course;
import com.think.teaching.model.CourseTag;
import com.think.teaching.model.Tag;
import com.think.teaching.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class CourseServiceImpl extends BaseServiceImpl<CourseMapper, Course> implements CourseService {

	private final CourseTagServiceImpl courseTagService;

	private final TagServiceImpl tagService;

	public CourseServiceImpl(CourseTagServiceImpl courseTagService, TagServiceImpl tagService) {
		this.courseTagService = courseTagService;
		this.tagService = tagService;
	}

	@Transactional
	@Override
	public Course AddCourse(Course course) throws Exception {
		if (this.save(course)) {
			if (course.getTagIds() != null && course.getTagIds().size() > 0) {
				List<Tag> newTags = tagService.listByIds(course.getTagIds());
				for (Tag newTag : newTags) {
					if (!newTag.getType().equals(Tag.COURSE_TAG)) {
						throw new Exception("传入的标签类型错误，不是课程标签");
					}
				}
				List<CourseTag> tags = new ArrayList<>();
				for (int i = 0; i < course.getTagIds().size(); i++) {
					CourseTag tag = new CourseTag();
					tag.setCourseId(course.getId());
					tag.setTagId(course.getTagIds().get(i));

					tags.add(tag);
				}
				courseTagService.saveBatch(tags);
			}
			return course;
		}
		return null;
	}

	@Override
	@Transactional
	public Course UpdateCourse(Course course) throws Exception {
		if (this.updateById(course)) {
			if (course.getTagIds() != null && course.getTagIds().size() > 0) {
				List<Tag> newTags = tagService.listByIds(course.getTagIds());
				for (Tag newTag : newTags) {
					if (!newTag.getType().equals(Tag.COURSE_TAG)) {
						throw new Exception("传入的标签类型错误，不是课程标签");
					}
				}
				List<CourseTag> tags = new ArrayList<>();
				for (int i = 0; i < course.getTagIds().size(); i++) {
					CourseTag tag = new CourseTag();
					tag.setCourseId(course.getId());
					tag.setTagId(course.getTagIds().get(i));

					tags.add(tag);
				}
				QueryWrapper<CourseTag> delete = new QueryWrapper<>();
				delete.eq("course_id", course.getId());
				courseTagService.remove(delete);
				courseTagService.saveBatch(tags);
			}
			return course;
		}
		return null;
	}

	@Override
	public Course GetCourse(Integer id, Integer tempFlag, Integer state) {

		return baseMapper.getCourseDetails(id, tempFlag, state);
	}

	@Override
	public List<Course> SearchCourseWithKey(String key, Boolean withoutBan) {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("course_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	public IPage<Course> ListCourseWithTagAndType(Integer count, Integer page, Integer type, Integer tempFlag,
			Integer mode, Integer state, Integer tag, Integer userId) {
		IPage<Course> ipage = new Page<>(page, count);
		return this.baseMapper.adminSelectCourseWithFilter(ipage, type, tag, state, mode, tempFlag, userId);
	}

	public IPage<Course> ListCourseBaseInfoWithTagAndType(Integer count, Integer page, Integer type, Integer tempFlag,
			Integer mode, Integer state, Integer tag, Integer userId) {
		IPage<Course> ipage = new Page<>(page, count);
		return this.baseMapper.adminSelectCourseBaseInfoWithFilter(ipage, type, tag, state, mode, tempFlag, userId);
	}

	public IPage<Map<String, Object>> OpenListCourseWithTagAndType(Integer count, Integer page, Integer type,
			Integer mode, Integer tag) {
		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		return this.baseMapper.openSelectCourseWithFilter(ipage, type, tag, mode);
	}

	@Override
	public Course SetCourseState(Integer id, Integer state) {
		UpdateWrapper<Course> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	public Boolean CheckCourseExist(Integer courseId, boolean withoutBan) {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("id", courseId);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return this.getOne(queryWrapper) != null;
	}

	public IPage<Course> GetUserSelectedCourseWithFilter(Integer userId, Integer count, Integer page, Integer type,
			Integer mode, Integer tag) {
		IPage<Course> ipage = new Page<>(page, count);
		return this.baseMapper.getUserSelectedCourseWithFilter(ipage, userId, type, mode, tag);
	}

	public List<Map<String, Object>> GetRecommondCourseList(Integer count) {
		return baseMapper.getRecommondCourse(count);
	}

	public int GetPassedReviewCount() {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("state", 1);
		return this.count(queryWrapper);
	}

	public int GetDuringReviewCount() {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("state", 3);
		return this.count(queryWrapper);
	}

	public int GetBanCount() {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("state", 0);
		return this.count(queryWrapper);
	}
}
